package com.core.ui.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.core.managers.ServletManager;
import com.core.protocols.IHibernateParentEnumeratorDelegate;
import com.core.protocols.IKVObject;
import com.core.protocols.IPageHeaderDatasource;
import com.core.servicelayer.HibernateServiceLayer;
import com.core.ui.controller.base.CustomController;
import com.core.utils.GlobalConstants.HibernateSQLAction;
import com.core.utils.StackItem;
import com.model.KeyValue;
import com.model.project.ProjectItem;
import com.model.project.ProjectTask;

public class StackNavigatorController
        extends CustomController
        implements IPageHeaderDatasource, IHibernateParentEnumeratorDelegate {

	private List <StackItem>                        list              = new ArrayList <StackItem>();

	private Map <String, Map <String, String>>      entityRelations   = new HashMap <String, Map <String, String>>();

	private Class <? extends HibernateServiceLayer> serviceLayerClass = null;
	private String                                  entityIdValue     = null;
	private String                                  pageCaption       = null;

	HibernateServiceLayer                           serviceLayer      = null;



	// private IStackNavigatorDelegate delegate;



	public StackNavigatorController() {

		Map <String, String> relation = null;
		relation = new HashMap <String, String>();
		relation.put(ProjectTask.class.getCanonicalName(), ProjectItem.class.getCanonicalName());
		entityRelations.put("TaskProject", relation);

		relation = new HashMap <String, String>();
		relation.put(ProjectTask.class.getCanonicalName(), ProjectTask.class.getCanonicalName());
		entityRelations.put("TaskTask", relation);



	}



	public Class <? extends HibernateServiceLayer> getServiceLayerClass () {

		return serviceLayerClass;
	}



	public void setServiceLayerClass ( Class <? extends HibernateServiceLayer> serviceLayerClass ) {

		this.serviceLayerClass = serviceLayerClass;
	}



	public String getEntityIdValue () {

		return entityIdValue;
	}



	public void setEntityIdValue ( String entityIdValue ) {

		this.entityIdValue = entityIdValue;
	}



	public void addStackItem ( StackItem item ) {

		list.add(item);
	}



	@Override
	public String pageCaption () {

		return this.pageCaption;
	}



	@Override
	public String pageAvatar () {

		String result = null;

		this.serviceLayerNewInstance();

		if ( serviceLayer == null ) {
			return null;
		}

		ServletManager servletManager = ServletManager.sharedInstance();
		result = servletManager.avatarNameForEntity(serviceLayer.entityClass(), HibernateSQLAction.SELECT);

		return result;
	}



	@Override
	public Iterator <StackItem> iterator () {

		return list.iterator();
	}



	@Override
	public List <StackItem> itemsList () {

		return list;
	}



	private void serviceLayerNewInstance () {

		if ( serviceLayer != null ) {
			return;
		}

		if ( serviceLayerClass == null ) {
			return;
		}

		try {
			serviceLayer = serviceLayerClass.newInstance();
		} catch ( InstantiationException e ) {
			e.printStackTrace();
		} catch ( IllegalAccessException e ) {
			e.printStackTrace();
		}

	}



	@Override
	public void reload () {

		this.serviceLayerNewInstance();

		if ( serviceLayer == null ) {
			return;
		}

		Integer objectUNID = null;
		try {
			objectUNID = Integer.parseInt(this.getEntityIdValue());
		} catch ( NumberFormatException e ) {

		}

		if ( objectUNID == null ) {
			return;
		}



		IKVObject kvObject = serviceLayer.objectByUNID(objectUNID, null, null);
		this.drawCaption(kvObject);
		this.drawParentTree(serviceLayer, kvObject);
	}



	private void drawParentTree ( HibernateServiceLayer serviceLayer,
	                              IKVObject object ) {


		serviceLayer.enumerateParentObjectsWithDelegate(object, this);

	}



	@Override
	public void onEnumarateParent ( IKVObject parent ) {

		if ( parent == null ) {
			return;
		}

		ServletManager servletManager = ServletManager.sharedInstance();
		String servletName = servletManager.servletNameForClassAction(parent.getClass(), HibernateSQLAction.SELECT);
		String url = String.format("%s?%s", servletName, parent.idValue().toString());

		StackItem stackItem = new StackItem(parent.nameValue().getValue(), url);
		this.addStackItem(stackItem);

	}



	private void drawCaption ( IKVObject kvObject ) {

		if ( kvObject == null ) {
			return;
		}

		String localizableDescript = kvObject.localizableDescription();
		String name = "";
		KeyValue nameValue = kvObject.nameValue();
		if ( nameValue != null ) {
			name = nameValue.getValue();
		}
		if ( name == null ) {
			this.pageCaption = String.format("%s: %s", localizableDescript, "n/a");
		} else {
			this.pageCaption = String.format("%s: %s", localizableDescript, name);
		}

	}



}
